package com.basic.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;

public class ParseDataUtil {
	
	public static String objToJson(Object obj){
		Map<String, Object> objData = new HashMap<String, Object>();
		Method[] methods = obj.getClass().getDeclaredMethods();
		for (Method method : methods) {
			if(method.getName().startsWith("get")){
				try {
					String key = method.getName().replace("get", "").substring(0, 1).toLowerCase()+method.getName().replace("get", "").substring(1, method.getName().replace("get", "").length());
					String value = (String) method.invoke(obj, null);
					objData.put(key, value);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return new Gson().toJson(objData);
	}
	
	public static Object jsonToObj(String jsonStr, Class tclass){
		Gson gson = new Gson();
		Object obj = gson.fromJson(jsonStr, tclass);
		return obj;
	}
}
